<?php
namespace app\admin\controller;
use think\Controller;

class Bis extends Controller
{
    private $obj;

    public function _initialize(){
        $this->obj = model('Bis');
    }

    public function index(){
        $bis = $this->obj->getBisByStatus(1);
        return $this->fetch('',[
            'bis' => $bis,
        ]);
    }

    public function apply()
    {
        $bis = $this->obj->getBisByStatus();
        return $this->fetch('',[
            'bis' => $bis,
        ]);
    }

    public function detail(){

        $id = input('get.id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        //获取一级城市的数据
        $citys = model('city')->getCitysByParentId();
        //获取一级栏目的数据
        $categorys = model('category')->getCategorysByParentId();
        //获取商户数据
        $bisData = model('bis')->get($id);
        $locationData = model('BisLocation')->get(['bis_id' => $id,'is_main' => 1]);
        $accountData = model('BisAccount')->get(['bis_id' => $id,'is_main' => 1]);
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'bisData' => $bisData,
            'locationData' => $locationData,
            'accountData' => $accountData,
        ]);
        return $this->fetch();
    }

    public function status(){
        $data = input('get.');

        //数据校验
        // $validate = validate('bis');
        // if(!$validate->scene('status')->check($data)){
        //     $this->error($validate->getError());
        // }

        $res = $this->obj->save(['status' => $data['status']],['id' => $data['id']]);
        $location = model('BisLocation')->save(['status' => $data['status']],['bis_id' => $data['id'],'is_main'=>1]);
        $account = model('BisAccount')->save(['status' => $data['status']],['bis_id' => $data['id'],'is_main' =>1]);
        if($res && $location && $account){
            //发送邮件
            //status 1 通过  status 0 待审  status 2 不通过 status -1 删除
            //\phpmailer\Email::send($to,$title,$content);
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
}
